﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ADService;
using System.Web.Security;
using System.IO;
using Avatar.Common;
using AvatarHelper;

public partial class Management_ADPublish : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                ddl_ADCategory.DataBind();
                var _adCategory = _adc.ADCategory.Where(c => c.ID == new Guid(ddl_ADCategory.SelectedValue)).FirstOrDefault();
                lbl_WidthHeight.Text = _adCategory.Width.ToString() + "*" + _adCategory.Height.ToString();
                ddl_Period.DataSource = _adCategory.ADPrice.Join(_adc.Period, price => price.PeriodID, period => period.ID, (price, period) => new { Name = period.Name, ID = period.ID });
                ddl_Period.DataTextField = "name";
                ddl_Period.DataValueField = "ID";
                ddl_Period.DataBind();
            }
        }
    }
    protected void btn_Next_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_Company.Value)) throw new Exception("先选择发布广告的企业");

            Guid _id = Guid.Empty;
            string _user = txt_Company.Value;

            #region 入口检测

            if (!FileUpload1.HasFile) throw new Exception("*请选择要上传的文件");

            Boolean _fileOK = false;
            String _fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] _allowedExtensions = { ".gif", ".jpeg", ".jpg" };
            foreach (var _e in _allowedExtensions)
            {
                if (_fileExtension.ToLower() == _e.ToLower())
                {
                    _fileOK = true;
                }
            }
            if (_fileOK != true) throw new Exception("*文件类型错误：只能上传是扩展名为:gif、jpeg、jpg的文件.");

            #endregion

            Guid _adCategory = new Guid(ddl_ADCategory.SelectedValue);

            #region 图片上传

            var _year = DateTime.Now.Year.ToString();
            var _month = DateTime.Today.Month.ToString();
            var _day = DateTime.Today.Day.ToString();
            var _hour = DateTime.Now.Hour.ToString();
            var _minute = DateTime.Now.Minute.ToString();
            var _second = DateTime.Now.Second.ToString();

            string _path = "~/ad/" + _year + "/" + _month + "/" + _day + "/" + _hour + "/";
            String _savePath = Server.MapPath("~/ad/") + _year + "\\" + _month + "\\" + _day + "\\" + _hour + "\\";
            Directory.CreateDirectory(_savePath);

            string _fileName = Guid.NewGuid().ToString() + _fileExtension;

            string _iconURL = _savePath + _fileName;


            FileUpload1.PostedFile.SaveAs(_iconURL);

            #endregion

            ADManager.ADPublishAdmin(
                txt_Title.Text,
                _adCategory,
                _user,
                _path + _fileName,
                txt_URL.Text,
                Convert.ToInt32(ddl_Period.SelectedValue));

            Helper.ShowMassage("广告发布成功！", Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
        finally
        {
            txt_Title.Text = "";
            FileUpload1.Dispose();
            ddl_Period.Dispose();
        }
    }
    protected void ddl_ADCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            var _adCategory = _adc.ADCategory.Where(c => c.ID == new Guid(ddl_ADCategory.SelectedValue)).FirstOrDefault();
            lbl_WidthHeight.Text = _adCategory.Width.ToString() + "*" + _adCategory.Height.ToString();
            ddl_Period.DataSource = _adCategory.ADPrice.Join(_adc.Period, price => price.PeriodID, period => period.ID, (price, period) => new { Name = period.Name, ID = period.ID });
            ddl_Period.DataTextField = "name";
            ddl_Period.DataValueField = "ID";
            ddl_Period.DataBind();
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        this.Search();
    }
    protected void Search()
    {
        try
        {

            if (string.IsNullOrEmpty(txt_KeyWord.Text)) throw new Exception("关键字不能为空");

            using (var _adc = DBHelper.GetDataContext())
            {
                gv_Company.DataSource = _adc.CompanyInformation.Where(i => i.CompanyName.Contains(txt_KeyWord.Text));
                gv_Company.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }

    protected void btn_Select_Click(object sender, EventArgs e)
    {
        var _btn = sender as Button;
        txt_Company.Value = _btn.CommandArgument;
        using (var _adc = DBHelper.GetDataContext())
        {
            lbl_Company.Text = _adc.CompanyInformation.Where(i => i.User == _btn.CommandArgument).Select(i => i.CompanyName).FirstOrDefault();
        }
    }
}
